home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue26 / chessbrd / CHESSBRD.ZIP / CPP / CHESSBRD.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-08  |  10.9 KB  |  244 lines

  1. //---------------------------------------------------------------------------
  2. #ifndef chessH
  3. #define chessH
  4. //---------------------------------------------------------------------------
  5. #include <vcl\SysUtils.hpp>
  6. #include <vcl\Controls.hpp>
  7. #include <vcl\Classes.hpp>
  8. #include <vcl\Forms.hpp>
  9. #include "CHTYPES.H"
  10. #include "CHTHREAD.H"
  11. //---------------------------------------------------------------------------
  12.  
  13. class MoveInfo
  14. {
  15.     public:
  16.     __fastcall MoveInfo(void);
  17.  
  18.     char position[66];
  19.     CastleSet Castling;
  20.     Square OldSquare, NewSquare;
  21.     Square EnPassant;
  22. };
  23.  
  24. class TChessBoard : public TGraphicControl
  25. {
  26. private:
  27.     TThreadPriority _ThinkingPriority;
  28.     bool _Thinking;
  29.     char position[66];
  30.     CastleSet _CastlingAllowed;
  31.     int _SearchDepth;
  32.  
  33.     ChessThread *Now;
  34.     Square _EnPassant;
  35.     MoveInfo MoveList[256][2];
  36.  
  37.     int _ResizeMinSize,_ResizeMaxSize;
  38.  
  39.     bool ResizeState, _Resizable;
  40.     char PromoteTo;
  41.     int PieceIndex[2][6];
  42.  
  43.     int Boardx,Boardy,BoardSize;
  44.     int PieceSize;
  45.     int _SizeOfSquare;
  46.     int _CurrentMove;
  47.  
  48.     StandardSet _StandardSize;
  49.  
  50.     TMoveEvent legalmove,check,mate,stalemate,castling;
  51.     TNotifyEvent onlytwokingsleft,threefoldposition;
  52.     TCaptureEvent capture;
  53.     TOneSquareEvent illegalmove;
  54.     TPromotionEvent promotion;
  55.  
  56.     String posstr;
  57.  
  58.     TImageList *list;
  59.     Graphics::TBitmap *_SquareLight,*_SquareDark, *_BorderBitmap;
  60.     Graphics::TBitmap *_CustomPieceSet,  *Default;
  61.     TPen *_LineStyle;
  62.     TFont *_CoordFont;
  63.  
  64.     CoordSet _DisplayCoords;
  65.  
  66.     int _SizeOfBorder;
  67.     Square SquareClick1,SquareClick2;
  68.     bool _ComputerPlaysWhite, _ComputerPlaysBlack;
  69.  
  70.     bool _WhiteOnTop,_WhiteToMove,_BoardLines;
  71.     TColor _SquareColorLight, _SquareColorDark, _BorderColor;
  72.  
  73.     void __fastcall FinishedThread (TObject *Sender);
  74.  
  75.     bool __fastcall MoveIsLegal (Square oldSq, Square newSq);
  76.     bool __fastcall CheckForThreefoldPosition(void);
  77.     bool __fastcall CheckLegalBishopMove(Square oldSq,Square newSq);
  78.     bool __fastcall CheckLegalKingMove(Square oldSq, Square newSq);
  79.     bool __fastcall CheckLegalKnightMove(Square oldSq,Square newSq);
  80.     bool __fastcall CheckLegalPawnMove(Square oldSq,Square newSq);
  81.     bool __fastcall CheckLegalQueenMove(Square oldSq,Square newSq);
  82.     bool __fastcall CheckLegalRookMove(Square oldSq,Square newSq);
  83.     bool __fastcall BitmapExists(Graphics::TBitmap *bmp);
  84.  
  85.     int  __fastcall PieceToInt (char piece);
  86.     void __fastcall DoPromotion (Square sq);
  87.     void __fastcall DrawBoard (void);
  88.     void __fastcall DrawBorder (void);
  89.     void __fastcall DrawPiece (Square sq, char piece);
  90.     void __fastcall DrawPieces (void);
  91.     void __fastcall Fillint (Square sq);
  92.     void __fastcall InitializeBitmaps (void);
  93.     void __fastcall OrganizeBitmaps(void);
  94.  
  95.     //boring write methods
  96.     void __fastcall set_BoardLines(bool show);
  97.     void __fastcall set_BorderBitmap(Graphics::TBitmap *bmp);
  98.     void __fastcall set_BorderColor(TColor c);
  99.     void __fastcall set_SizeOfBorder (int border);
  100.     void __fastcall set_ComputerPlaysBlack (bool computerplays);
  101.     void __fastcall set_ComputerPlaysWhite (bool computerplays);
  102.     void __fastcall set_CoordFont(TFont *font);
  103.     void __fastcall set_CurrentMove (int moveno);
  104.     void __fastcall set_CustomPieceSet(Graphics::TBitmap *bmp);
  105.     void __fastcall set_DisplayCoords(CoordSet set);
  106.     void __fastcall set_EnPassant (Square sq);
  107.     void __fastcall set_Height(int hgt);
  108.     void __fastcall set_LineStyle(TPen *pen);
  109.     void __fastcall set_ResizeMaxSize (int size);
  110.     void __fastcall set_ResizeMinSize (int size);
  111.     void __fastcall set_PosStr(String pos);
  112.     void __fastcall set_SearchDepth (int depth);
  113.     void __fastcall set_SizeOfSquare(int size);
  114.     void __fastcall set_SquareColorDark(TColor c);
  115.     void __fastcall set_SquareColorLight(TColor c);
  116.     void __fastcall set_SquareDark(Graphics::TBitmap *bmp);
  117.     void __fastcall set_SquareLight(Graphics::TBitmap *bmp);
  118.     void __fastcall set_StandardSize(StandardSet set);
  119.     void __fastcall set_Thinking (bool thinking);
  120.     void __fastcall set_ThinkingPriority (TThreadPriority priority);
  121.     void __fastcall set_WhiteOnTop(bool wabove);
  122.     void __fastcall set_WhiteSquareBitmap(Graphics::TBitmap *bmp);
  123.     void __fastcall set_WhiteToMove(bool wmove);
  124.     void __fastcall set_Width(int wdt);
  125.  
  126. protected:
  127.     //Event handlers
  128.     virtual void __fastcall Capture (System::TObject *Sender,  Square oldSq, Square newSq, char CapturedPiece);
  129.     virtual void __fastcall Castle (System::TObject *Sender, Square oldSq, Square newSq);
  130.     virtual void __fastcall Check (System::TObject *Sender,  Square oldSq, Square newSq);
  131.     virtual void __fastcall Click(void);
  132.     virtual void __fastcall DragCanceled(void);
  133.     virtual void __fastcall DragDrop(System::TObject *Source,int X, int Y);
  134.     virtual void __fastcall DragOver(System::TObject* Source, int X, int Y,TDragState State, bool &Accept );
  135.     virtual void __fastcall EndDrag(bool drop);
  136.     virtual void __fastcall IllegalMove (System::TObject *Sender,  Square sq);
  137.     virtual void __fastcall LegalMove (System::TObject *Sender,  Square oldSq, Square newSq);
  138.     virtual void __fastcall Mate (System::TObject *Sender,  Square oldSq, Square newSq);
  139.     virtual void __fastcall MouseDown(TMouseButton Button, Classes::TShiftState Shift, int X, int Y);
  140.     virtual void __fastcall MouseMove(Classes::TShiftState Shift, int X, int Y);
  141.     virtual void __fastcall MouseUp(TMouseButton Button, Classes::TShiftState Shift, int X, int Y);
  142.     virtual void __fastcall OnlyTwoKingsLeft (System::TObject *Sender);
  143.     virtual void __fastcall ThreefoldPosition (System::TObject *Sender);
  144.     virtual void __fastcall Paint(void);
  145.     virtual void __fastcall Promotion (System::TObject *Sender,  Square oldSq, Square newSq, char &NewPiece);
  146.     virtual void __fastcall StaleMate (System::TObject *Sender,  Square oldSq, Square newSq);
  147.     virtual void __fastcall WndProc (TMessage &Message);
  148.  
  149. public:
  150.  
  151.     int FirstMove, LastMove;
  152.     bool FirstTurn, LastTurn;
  153.  
  154.     //Constructor, destructor
  155.     __fastcall TChessBoard(TComponent* Owner);
  156.     virtual __fastcall ~TChessBoard(void);
  157.  
  158.     //Public interface
  159.     void __fastcall MoveNow (void);
  160.     void __fastcall CancelThinking(void);
  161.     void __fastcall DrawChessPiece(TCanvas *canvas, int x, int y,char piece);
  162.     bool __fastcall BlackInCheckAfter(Square oldSq, Square newSq);
  163.     bool __fastcall ClearSquare(Square sq);
  164.     static int inline __fastcall ColorOfPiece (char piece);
  165.     int  __fastcall ColorOfPiece (Square sq);
  166.     int  __fastcall ColorOfSquare (Square sq);
  167.     MoveInfo __fastcall GetMove (int moveno, bool WhiteMoves);
  168.     TStringList* __fastcall GetMoveList (void);
  169.     bool __fastcall GotoMove (int moveno, bool WhiteMoves);
  170.     bool __fastcall LegalMoveAvailable (void);
  171.     static Square inline __fastcall MouseToSquare (int x, int y);
  172.     bool __fastcall Move (Square , Square );
  173.     bool __fastcall MoveBackward (void);
  174.     bool __fastcall MoveForward (void);
  175.     void __fastcall NewGame(void);
  176.     bool __fastcall SetUpPosition (MoveInfo &pos, int moveno, bool whitemoves);
  177.     void __fastcall UpdateChessBoard (char *oldpos);
  178.     bool __fastcall WhiteInCheckAfter(Square oldSq, Square newSq);
  179.     Square __fastcall WindowToSquare (int x, int y);
  180.     static int inline __fastcall XPos (Square sq);
  181.     static int inline __fastcall YPos (Square sq);
  182.  
  183. __published:
  184.  
  185.     __property bool BoardLines={read=_BoardLines,write=set_BoardLines};
  186.     __property bool ComputerPlaysBlack={read=_ComputerPlaysBlack, write=set_ComputerPlaysBlack};
  187.     __property bool ComputerPlaysWhite={read=_ComputerPlaysWhite, write=set_ComputerPlaysWhite};
  188.     __property bool Resizable={read=_Resizable, write=_Resizable};
  189.     __property bool Thinking={read=_Thinking,write=set_Thinking};
  190.     __property bool WhiteOnTop={read=_WhiteOnTop,write=set_WhiteOnTop};
  191.     __property bool WhiteToMove={read=_WhiteToMove,write=set_WhiteToMove,default=TRUE};
  192.     __property CastleSet CastlingAllowed={read=_CastlingAllowed,write=_CastlingAllowed};
  193.     __property CoordSet DisplayCoords={read=_DisplayCoords,write=set_DisplayCoords};
  194.     __property Graphics::TBitmap *BorderBitmap={read=_BorderBitmap, write=set_BorderBitmap};
  195.     __property Graphics::TBitmap *CustomPieceSet={read=_CustomPieceSet, write=set_CustomPieceSet};
  196.     __property Graphics::TBitmap *SquareDark={read=_SquareDark, write=set_SquareDark};
  197.     __property Graphics::TBitmap *SquareLight={read=_SquareLight, write=set_SquareLight};
  198.     __property int  SearchDepth={read=_SearchDepth,write=set_SearchDepth};
  199.     __property int CurrentMove={read=_CurrentMove, write=set_CurrentMove};
  200.     __property int ResizeMaxSize={read=_ResizeMaxSize, write=set_ResizeMaxSize};
  201.     __property int ResizeMinSize={read=_ResizeMinSize, write=set_ResizeMinSize};
  202.     __property int SizeOfBorder={read=_SizeOfBorder, write=set_SizeOfBorder};
  203.     __property int SizeOfSquare={read=_SizeOfSquare, write=set_SizeOfSquare};
  204.     __property Square EnPassant={read=_EnPassant, write=set_EnPassant};
  205.     __property StandardSet StandardSize={read=_StandardSize, write=set_StandardSize, default=Size40};
  206.     __property String Position={read=posstr, write=set_PosStr};
  207.     __property TColor BorderColor={read=_BorderColor, write=set_BorderColor};
  208.     __property TColor SquareColorDark={read=_SquareColorDark, write=set_SquareColorDark};
  209.     __property TColor SquareColorLight={read=_SquareColorLight, write=set_SquareColorLight};
  210.     __property TFont *CoordFont={read=_CoordFont, write=set_CoordFont};
  211.     __property TPen *LineStyle={read=_LineStyle, write=set_LineStyle};
  212.     __property TThreadPriority ThinkingPriority={read=_ThinkingPriority, write=set_ThinkingPriority};
  213.  
  214.     __property TMoveEvent OnCastle={read=castling, write=castling};
  215.     __property TMoveEvent OnCheck={read=check, write=check};
  216.     __property TMoveEvent OnLegalMove={read=legalmove, write=legalmove};
  217.     __property TMoveEvent OnMate={read=mate, write=mate};
  218.     __property TMoveEvent OnStaleMate={read=stalemate, write=stalemate};
  219.     __property TNotifyEvent OnOnlyTwoKingsLeft={read=onlytwokingsleft, write=onlytwokingsleft};
  220.     __property TNotifyEvent OnThreefoldPosition={read=threefoldposition, write=threefoldposition};
  221.     __property TOneSquareEvent OnIllegalMove={read=illegalmove, write=illegalmove};
  222.     __property TPromotionEvent OnPromotion={read=promotion, write=promotion};
  223.     __property TCaptureEvent OnCapture={read=capture, write=capture};
  224.  
  225.     __property Visible;
  226.     __property Enabled;
  227.  
  228.     __property DragCursor;
  229.     __property DragMode;
  230.     __property OnClick;
  231.     __property OnDblClick ;
  232.     __property OnDragDrop ;
  233.     __property OnDragOver ;
  234.     __property OnEndDrag ;
  235.     __property OnMouseDown ;
  236.     __property OnMouseMove ;
  237.     __property OnMouseUp;
  238.     __property OnStartDrag;
  239. };
  240.  
  241. //---------------------------------------------------------------------------
  242. #endif
  243.  
  244.